<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        let users = [
            { id: 100001, name: "张三", skill: "html" },
            { id: 100001, name: "张三", skill: "css" },
            { id: 100001, name: "张三", skill: "js" },
            { id: 100002, name: "李四", skill: "html" },
            { id: 100002, name: "李四", skill: "css" },
            { id: 100002, name: "李四", skill: "js" },
            { id: 100003, name: "王二", skill: "html" },
            { id: 100003, name: "王二", skill: "css" }
        ]
        // 转换为
        // let users = [
        //     { id: 100001, name: "张三", skills: ["css", "js", "html"] },
        //     { id: 100002, name: "李四", skills: ["css", "js"] },
        //     { id: 100003, name: "王二", skills: ["html", "css"] }
        // ]
        let userList = {};
        users.forEach((item) => {
            if (userList[item.id]) {
                // userList 中存在该ID的user对象
                userList[item.id].skills.push(item.skill);
            } else {
                item.skills = [item.skill];
                delete item.skill;
                userList[item.id] = item;
            }
        });
        userList = Object.values(userList);
        console.log(userList);

        let data = [
            { a: 1, b: 1, c: 1 },
            { a: 2, c: 2 },
            { b: 3, c: 3 },
            { a: 4 },
        ]
        // 转换为
        // let dataObj = {
        //     a: [1, 2, 4],
        //     b: [1, 3],
        //     c: [1, 2, 3]
        // }

        let dataObj = {};
        for (let item of data) {
            for (let key in item) {
                // if(dataObj[key]){
                //     dataObj[key].push(item[key])
                // }else{
                //     dataObj[key] = [];
                //     dataObj[key].push(item[key])
                // }
                if (!dataObj[key]) {
                    dataObj[key] = [];
                }
                dataObj[key].push(item[key]);
            }
        }
        console.log(dataObj);


    </script>
</head>

<body>

</body>

</html>